#ifndef MAINOPERATIONWIDGET_H
#define MAINOPERATIONWIDGET_H

#include <QWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QStackedWidget>

#include "ProcessWidget.h"
#include "GridTableWidget.h"
#include "SteelClientDataProvider.h"
#include <QVector>
#include "OxygenLanceWidget.h"
#include "ChartWidget.h"
#include "OperProcessWidget.h"
#include "DebugWidget.h"
#include "BlowingWidget.h"
#include "VideoWidget.h"

class MainOperationWidget : public QWidget
{
    Q_OBJECT

public:
    struct TableItemData {
        QString name;
        QString value;
    };

    explicit MainOperationWidget(QWidget *parent = nullptr);
    ~MainOperationWidget();

    void setCurrentStoveIndex(int stoveIndex);
    void setWorkingConditionData(const QVector<TableItemData> &items);
    void setMaterialData(const QVector<TableItemData> &items);
    void setOxygenLanceData(const MainPageOxygenLanceData &data);
    void setBlowingData(const MainPageBlowingData &data);
    void setChartData(const MainPageChartData &data);
    void setChartTableItem(int row, int column, const QString& value);
    QString chartTableItem(int row, int column) const;
    void setOperStageData(const MainPageOperStageData &data);
    void setOperStageItem(int row, int column, const QString& value);
    QString operStageItem(int row, int column) const;
    void setOperTotalData(const MainPageOperTotalData &data);
    void setOperTotalItem(int row, int column, const QString& value);
    QString operTotalItem(int row, int column) const;
    void setProcessData(const MainPageProcessData &data);
    void setVideoData(const MainPageVideoData &data);
    WId videoWindowHandle() const;
    VideoWidget* getVideoWidget() const;
    NavTreeIndex getCurrentNavIndex() const;

signals:
    void workingConditionItemChanged(int index, const QString &name, const QString &value);
    void materialItemChanged(int index, const QString &name, const QString &value);
    void oxygenLanceDataChanged(const MainPageOxygenLanceData &data);
    void blowingDataChanged(const MainPageBlowingData &data);

private:
    void setupUI();
    void setupConnections();
    void setupWorkingConditionData();
    void setupMaterialData();
    void applyWorkingConditionData(const MainPageWorkingConditionData &data);
    void applyMaterialData(const MainPageMaterialData &data);
    void applyOxygenLanceData(const MainPageOxygenLanceData &data);
    void applyBlowingData(const MainPageBlowingData &data);
    void applyChartData(const MainPageChartData &data);
    void applyOperStageData(const MainPageOperStageData &data);
    void applyOperTotalData(const MainPageOperTotalData &data);
    void applyProcessData(const MainPageProcessData &data);
    void applyVideoData(const MainPageVideoData &data);

    void registerCallbacks();
    void unregisterCallbacks();

    static void workingConditionCallback(NavTreeIndex index, const MainPageWorkingConditionData* data, void* userData);
    static void materialCallback(NavTreeIndex index, const MainPageMaterialData* data, void* userData);
    static void oxygenLanceCallback(NavTreeIndex index, const MainPageOxygenLanceData* data, void* userData);
    static void blowingCallback(NavTreeIndex index, const MainPageBlowingData* data, void* userData);
    static void chartCallback(NavTreeIndex index, const MainPageChartData* data, void* userData);
    static void operStageCallback(NavTreeIndex index, const MainPageOperStageData* data, void* userData);
    static void operTotalCallback(NavTreeIndex index, const MainPageOperTotalData* data, void* userData);
    static void processCallback(NavTreeIndex index, const MainPageProcessData* data, void* userData);
    static void videoCallback(NavTreeIndex index, const MainPageVideoData* data, void* userData);

    void handleWorkingConditionDataUpdate(NavTreeIndex index, const MainPageWorkingConditionData &data);
    void handleMaterialDataUpdate(NavTreeIndex index, const MainPageMaterialData &data);
    void handleOxygenLanceDataUpdate(NavTreeIndex index, const MainPageOxygenLanceData &data);
    void handleBlowingDataUpdate(NavTreeIndex index, const MainPageBlowingData &data);
    void handleChartDataUpdate(NavTreeIndex index, const MainPageChartData &data);
    void handleOperStageDataUpdate(NavTreeIndex index, const MainPageOperStageData &data);
    void handleOperTotalDataUpdate(NavTreeIndex index, const MainPageOperTotalData &data);
    void handleProcessDataUpdate(NavTreeIndex index, const MainPageProcessData &data);
    void handleVideoDataUpdate(NavTreeIndex index, const MainPageVideoData &data);

    void handleWorkingConditionValueChanged(int index, const QString &value);
    void handleMaterialValueChanged(int index, const QString &value);
    void handleOxygenLanceValueChanged(const QString &name, const QString &value);
    void handleDebugButtonClicked(DebugWidget::DebugButtonType buttonType, bool isPressed);

    QVBoxLayout *m_mainLayout;

    // 顶部布局
    QLabel *m_classInfoLabel;
    ProcessWidget *m_processWt;

    // 中间布局
    GridTableWidget *m_workingConditionWt;
    GridTableWidget *m_materialWt;
    VideoWidget *m_videoWt;
    BlowingWidget *m_blowingWt;

    // 底部布局
    OxygenLanceWidget *m_oxygenLanceWt;
    ChartWidget *m_chartWt;
    OperProcessWidget *m_operProcessWt;
    DebugWidget *m_debugWt;

    int m_currentStoveIndex = 0;
    NavTreeIndex m_currentNavIndex = NavTreeIndex::Main_1;
    int m_workingConditionCallbackId = -1;
    int m_materialCallbackId = -1;
    int m_oxygenLanceCallbackId = -1;
    int m_blowingCallbackId = -1;
    int m_chartCallbackId = -1;
    int m_operStageCallbackId = -1;
    int m_operTotalCallbackId = -1;
    int m_processCallbackId = -1;
    int m_videoCallbackId = -1;
    bool m_updatingWorkingCondition = false;
    bool m_updatingMaterial = false;
    bool m_updatingOxygen = false;
    bool m_updatingBlowing = false;
    bool m_updatingOperStage = false;
    bool m_updatingOperTotal = false;
    bool m_updatingProcess = false;
    bool m_updatingVideo = false;
};

#endif // MAINOPERATIONWIDGET_H
